function $ajax({
    method = "get",
    url,
    data,
    success,
    error
}) {
    //1.创建Ajax对象
    var xhr = null;
    try {
        xhr = new XMLHttpRequest();
    } catch (error) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    //判断如果数据存在
    if (data) {
        data = querystring(data);
    }
    if (method == "get") {
        url += "?" + data;
    }
    xhr.open(method, url, true);

    if (method == "get") {
        xhr.send();
    } else {
        //必须在调用send之前设置请求的格式
        xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        xhr.send(data);
    }

    //4.等待数据响应
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                if (success) {
                    success(xhr.responseText);
                }
            } else {
                if (error) {
                    error("Error" + xhr.status);
                }
            }
        }
    }
    //将对象转为字符串
    function querystring(obj) {
        var str = '';
        for (var attr in obj) {
            str += attr + "=" + obj[attr] + "&";
        }
        return str.substring(0, str.length - 1);
    }
}